package com.imooc.mall.controller;

import com.imooc.mall.common.ApiRestResponse;
import com.imooc.mall.filter.UserFilter;
import com.imooc.mall.model.vo.CartVo;
import com.imooc.mall.service.CartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author yijiu
 * @version 1.0
 * @description: 购物车Controller
 * @date 2022/3/14 23:36
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;

    @ApiOperation("购物车列表")
    @PostMapping("/list")
    public ApiRestResponse list() {
        //内部获取用户id，防止横向越权
        //使用UserFilter获取当前用户id
        List<CartVo> cartList = cartService.list(UserFilter.currentUser.getId());
        Integer userId = UserFilter.currentUser.getId();
        return ApiRestResponse.success(cartList);
    }

    @ApiOperation("添加商品到购物车")
    @PostMapping("/add")
    public ApiRestResponse add(@RequestParam Integer productId, @RequestParam Integer count) {
        Integer userId = UserFilter.currentUser.getId();
        List<CartVo> cartVoList = cartService.add(userId, productId, count);
        return ApiRestResponse.success(cartVoList);
    }

    @ApiOperation("更新商品到购物车")
    @PostMapping("/update")
    public ApiRestResponse update(@RequestParam Integer productId, @RequestParam Integer count) {
        Integer userId = UserFilter.currentUser.getId();
        List<CartVo> cartVoList = cartService.update(userId, productId, count);
        return ApiRestResponse.success(cartVoList);
    }

    @ApiOperation("删除商品从购物车")
    @PostMapping("/delete")
    public ApiRestResponse delete(@RequestParam Integer productId) {
        //不能传入userID，cartID，否则可以删除别人的购物车
        Integer userId = UserFilter.currentUser.getId();
        List<CartVo> cartVoList = cartService.delete(userId, productId);
        return ApiRestResponse.success(cartVoList);
    }

    @ApiOperation("选中/不购物车的某商品")
    @PostMapping("/select")
    public ApiRestResponse select(@RequestParam Integer productId,@RequestParam Integer selected) {
        //不能传入userID，cartID，否则可以删除别人的购物车
        Integer userId = UserFilter.currentUser.getId();
        List<CartVo> cartVoList = cartService.selectOrNot(userId, productId,selected);
        return ApiRestResponse.success(cartVoList);
    }

    @ApiOperation("全选择/全不选择购物车的某商品")
    @PostMapping("/selectAll")
    public ApiRestResponse selectAllOrNot(@RequestParam Integer selected) {
        //不能传入userID，cartID，否则可以删除别人的购物车
        Integer userId = UserFilter.currentUser.getId();
        List<CartVo> cartVoList = cartService.selectAllOrNot(userId,selected);
        return ApiRestResponse.success(cartVoList);
    }
}
